<template>
  <el-form :model="user" :rules="rules" label-width="80px" ref="form">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input placeholder="请输入旧密码" show-password type="password" v-model="user.oldPassword"/>
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input placeholder="请输入新密码" show-password type="password" v-model="user.newPassword"/>
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input placeholder="请确认新密码" show-password type="password" v-model="user.confirmPassword"/>
    </el-form-item>
    <el-form-item>
      <el-button @click="submit" size="mini" type="primary">保存</el-button>
      <el-button @click="close" size="mini" type="danger">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
    import {updateUserPwd} from "@/api/system/user";

    export default {
        data() {
            const equalToPassword = (rule, value, callback) => {
                if (this.user.newPassword !== value) {
                    callback(new Error("两次输入的密码不一致"));
                } else {
                    callback();
                }
            };
            return {
                user: {
                    oldPassword: undefined,
                    newPassword: undefined,
                    confirmPassword: undefined
                },
                // 表单校验
                rules: {
                    oldPassword: [
                        {required: true, message: "旧密码不能为空", trigger: "blur"}
                    ],
                    newPassword: [
                        {
                            required: true, message: "新密码不能为空,密码由8-20位数字、大小写字母和特殊符号组成!", trigger: "blur"
                            , pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,./]).{8,20}$/
                        },
                        /* { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }*/
                    ],
                    confirmPassword: [
                        {required: true, message: "确认密码不能为空", trigger: "blur"},
                        {required: true, validator: equalToPassword, trigger: "blur"}
                    ]
                }
            };
        },
        methods: {
            submit() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
                            this.$modal.msgSuccess("修改成功");
                        });
                    }
                });
            },
            close() {
                this.$tab.closePage();
            }
        }
    };
</script>
